package com.niiwoo.civet.trade.service.dubbo.credit;

import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.trade.dao.entity.credit.CreditProject;
import com.niiwoo.civet.trade.dto.request.credit.CreditLoanInvestProjectRequestDTO;
import com.niiwoo.civet.trade.dto.request.creditloan.MyCreditLoanRequestDTO;
import com.niiwoo.civet.trade.dto.response.MyCreditLoanResponseDTO;
import com.niiwoo.civet.trade.dto.response.credit.CreditProjectDTO;
import com.niiwoo.civet.trade.dto.response.credit.QueryCreditContractReponseDTO;
import com.niiwoo.civet.trade.dto.response.credit.QueryCreditInvestInfoReponseDTO;
import com.niiwoo.civet.trade.dto.response.credit.QueryCreditProjectDynamicReponseDTO;
import com.niiwoo.civet.trade.service.credit.CreditDubboService;
import com.niiwoo.civet.trade.service.local.credit.CreditProjectService;
import com.niiwoo.civet.trade.service.local.credit.CreditService;
import com.niiwoo.tripod.provider.dto.response.PageResponseDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * Created by daizhiyue on 2017/12/14.
 */
@Service(version = "1.0.0", validation = "true")
public class CreditDubboServiceImpl implements CreditDubboService {

    @Autowired
    CreditService creditService;
    @Autowired
    private CreditProjectService creditProjectService;
    @Override
    public PageResponseDTO<MyCreditLoanResponseDTO> myCreditLoanQuery(MyCreditLoanRequestDTO dto) {
        Assert.notNull(dto, "请求参数缺失");
        Assert.notNull(dto.getUserId(), "用户ID不能为空");
        try {
            return creditService.myCreditLoanQuery(dto);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 信用借查询回款记录
     *
     * @param userId
     * @return
     */
    @Override
    public QueryCreditProjectDynamicReponseDTO queryCreditProjectDynamic(Long projectId, String userId, Long opId, Byte type) {
        return creditService.queryProjectDynamic(projectId,userId,opId,type);
    }

    /**
     * 信用借查询投资记录
     *
     * @param requestDTO
     * @return
     */
    @Override
    public PageResponseDTO<QueryCreditInvestInfoReponseDTO> queryCreditInvestInfoByProjectId(CreditLoanInvestProjectRequestDTO requestDTO) {

        return creditService.queryCreditProjectInfoList(requestDTO);
    }
    /**
     * 信用借查询合同参数
     *
     * @param userId
     * @return
     */
    @Override
    public QueryCreditContractReponseDTO queryCreditContractParas(Long projectId, String userId, byte type) {

        return creditService.queryCreditContractParms(projectId, userId, type) ;
    }

    /**
     * 判断用户是否存在信用借
     */
    @Override
    public int queryCreditInvestorProject(String userId) {

        return creditService.queryCreditInvestorProject(userId);
    }

    @Override
    public CreditProjectDTO getCreditProjectById(Long projectId){
        CreditProjectDTO creditProjectDTO = null;

        List<CreditProject> creditProjectList = creditProjectService.getCreditProjectByProjectIdList(Arrays.asList(projectId));
        if(!CollectionUtils.isEmpty(creditProjectList)){
            CreditProject creditProject = creditProjectList.get(0);
            if(Objects.nonNull(creditProject)){
                creditProjectDTO = new CreditProjectDTO();
                BeanUtils.copyProperties(creditProject, creditProjectDTO);
            }
        }

        return creditProjectDTO;
    };
}
